package cn.net.wanji.videoandetc.utils;

import cn.net.wanji.videoandetc.pojo.FrameEtc;
import cn.net.wanji.videoandetc.pojo.FrameVideo;
import cn.net.wanji.videoandetc.pojo.Param;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xinyingjie
 * @description
 * @date 2022/8/21
 */


public class VideoWithEtc {
    //统计时间为10分钟
    public static final long COUNTTIME = 60 * 10;
    //最大时间判断阈值为5s
    public static final long MAXTIME = 5;

    public List<FrameVideo> test(Param param, FrameVideo video, FrameEtc etc){
        //video统计数量为真值
        int videoCount = video.getCount();
        //video交易时间为真值
        long videoTime = video.getTime();
        //video汽车个数量为真值
        String videoCarType = video.getCarType();
        //根据相邻帧率的时间差计算累计统计时间
        if (param.getTimeNow() == 0){
            param.setTimeNow(videoTime);
        }else {
            param.setTimeDiff(videoTime - param.getTimeNow());
            param.setTimeAll(param.getTimeAll()+ param.getTimeDiff());
            param.setTimeNow(videoTime);
        }
        if (param.getTimeAll() <= COUNTTIME){
            //如果车辆为特殊车型或者时间间隔大于设定时间
            if (!videoCarType.equals("car") || param.getTimeDiff() > MAXTIME ){
                //如果两类数据的车型一直或者颜色一致
                if (video.getCarType().equals(etc.getCarType()) || video.getColors().containsKey(etc.getColor())
                || video.getLaneNum() == etc.getLaneNum()){
                    //如果两个缓存的车辆个数一致，那么直接将etc数据的车牌赋值给Video,并将Video结果保存到真值数据中
                    if (param.getVideoList().size() == param.getEtcList().size()){
                        for (int i = 0; i < param.getVideoList().size(); i++) {
                            FrameVideo frameVideo = param.getVideoList().get(i);
                            FrameEtc frameEtc = param.getEtcList().get(i);
                            param = addResult(frameVideo, frameEtc, param);
                        }

                        param = addResult(video, etc, param);
                        //清空缓存和临时变量，重新开始
                        param = deleteCache(param);

                    }else {
                        //因为Video的数量为真值，所以当video数量小于Etc的时候，说明了出现了重复交易
                        List<FrameVideo> videoList = param.getVideoList();
                        List<FrameEtc> etcList = param.getEtcList();
                        if (videoList.size() < etcList.size()){
                            System.out.println("重复交易");
                            int videoIndex = 0;
                            int etcIndex = 0;
                            while (videoIndex < videoList.size() && etcIndex < etcList.size()){
                                FrameVideo frameVideo = videoList.get(videoIndex);
                                FrameEtc frameEtc = etcList.get(etcIndex);
                                Boolean is = isSame(frameVideo,frameEtc);
                            }

                        }
                    }
                }

                //如果车辆为普通车型或者时间间隔小于设定时间
            }else {
                param = addCache(video,etc,param);

            }
        }
        return param.getResult();

    }

    /**
     * 判断两条数据是否一致
     * @param frameVideo
     * @param frameEtc
     * @return
     */
    private Boolean isSame(FrameVideo frameVideo, FrameEtc frameEtc) {
        Map<String, Integer> videoColors = frameVideo.getColors();
        String etcColor = frameEtc.getColor();

        return null;
    }

    /**
     * 并不是特殊车型或者长时间间隔，将数据进行缓存
     * @param video
     * @param etc
     * @param param
     * @return
     */
    private Param addCache(FrameVideo video, FrameEtc etc, Param param) {
        List<FrameVideo> videoList = param.getVideoList();
        List<FrameEtc> etcList = param.getEtcList();
        videoList.add(video);
        etcList.add(etc);
        param.setVideoList(videoList);
        param.setEtcList(etcList);
        return param;
    }

    /**
     * 匹配成功，将车牌赋予给video,并保存结果
     * @param frameVideo
     * @param frameEtc
     * @param param
     * @return
     */
    private Param addResult(FrameVideo frameVideo, FrameEtc frameEtc, Param param) {
        frameVideo.setCarNum(frameEtc.getCarNum());
        List<FrameVideo> result = param.getResult();
        result.add(frameVideo);
        param.setResult(result);
        return param;
    }

    /**
     * 一次匹配，全部匹配成功后，删除所有缓存
     * @param param
     * @return
     */
    private Param deleteCache(Param param) {
        param.setTimeAll(0);
        param.setTimeDiff(0);
        param.setTimeNow(0);
        param.setVideoList(new ArrayList<>());
        param.setEtcList(new ArrayList<>());
        return param;
    }
}
